package com.hualu.xmlCode.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.tnc.dao.hibernate.GenericDaoHibernate;

import com.hualu.xmlCode.dao.AlbumViewDao;
import com.hualu.xmlCode.model.AlbumView;
import com.hualu.xmlCode.model.AlbumViewSearchCondition;

public class AlbumViewDaoHibernate extends GenericDaoHibernate<AlbumView, Integer> implements AlbumViewDao {

    public AlbumViewDaoHibernate() {
        super(AlbumView.class);
    }

    public List<AlbumView> getAlbumViewListByCondition(AlbumViewSearchCondition albumViewSearchCondition) {

        DetachedCriteria c = DetachedCriteria.forClass(AlbumView.class);
        List<AlbumView> list = new ArrayList<AlbumView>();

        // 电影类别, 1 电影 , 2 电视剧, 3 片花, 4 红色经典
        if (albumViewSearchCondition.getVideoType() != null) {
            if (albumViewSearchCondition.getVideoType() == 1) {
                c.add(Restrictions.eq("videoType", 1));
            }
            if (albumViewSearchCondition.getVideoType() == 2) {
                c.add(Restrictions.eq("videoType", 2));
            }
            if (albumViewSearchCondition.getVideoType() == 3) {
                c.add(Restrictions.eq("videoType", 3));
            }
            if (albumViewSearchCondition.getVideoType() == 4) {
                c.add(Restrictions.eq("videoType", 4));
            }
        }

        // 视频名称
        if (StringUtils.isNotBlank(albumViewSearchCondition.getVideoName())) {
            c.add(Restrictions.like("videoName", "%" + albumViewSearchCondition.getVideoName() + "%"));
        }

        list = this.findByCriteria(c);
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<AlbumView> findByCriteria(final DetachedCriteria detachedCriteria) {
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                return criteria.list();
            }
        });
    }
}
